package com.demo.controller;

import com.demo.pojo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TestController {

    /**
     * 接收页面请求的JSON数据，并返回JSON格式结果
     */
    @RequestMapping("/testJson")
    @ResponseBody
//    public Person testJson(@RequestBody Person user) {
//    public ArrayList<Person> testJson(@RequestBody Person user) {
//    public Map<String, Object> testJson(@RequestBody Person user) {
//    public List<Map<String, Object>> testJson(@RequestBody Person user) {
    public List<Map<String, Object>> testJson(@RequestBody List<Person> users) {
        //打印接受到的JSON格式数据
        //1.返回单个Person
//        System.out.println("username = " + user.getUsername());
//        System.out.println("password = " + user.getPassword());
//        System.out.println("age = " + user.getAge());

//        return user;
        //2.返回ArrayList<Person>
//        ArrayList<Person> list = new ArrayList<>();
//        list.add(new Person("admin1", "123", 13));
//        list.add(new Person("admin2", "456", 14));
//        list.add(new Person("admin3", "789", 15));
//        return list;
        //3.返回Map<String, Object>
        Map<String, Object> map = new HashMap<>();
        map.put("username", "admin4");
        map.put("password", "123456");
        map.put("age", 12);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("username", "admin5");
        map2.put("password", "789");
        map2.put("age", 20);
//        return map;
        //4.返回List<Map<String,Object>>
        List<Map<String, Object>> list = new ArrayList<>();
        list.add(map);
        list.add(map2);
        //5.前端传来List<Person>
        System.out.println(users);
        return list;
    }
}
